﻿using System.Collections.Generic;
using System.Xml;
using Assets.AnimationPlayer.Scripts.Util;

namespace Assets.AnimationPlayer.Scripts.Animations
{
    public class AnimationCfg
    {
        public AnimationCfg()
        {
            AnimationObjs = new List<AnimationObj>();
        }

        #region property

        public List<AnimationObj> AnimationObjs { get; set; }

        #endregion

        #region search

        public AnimationObj GetAnimationObjById(string aId)
        {
            foreach (AnimationObj animationObj in AnimationObjs)
            {
                if (animationObj.Id.Equals(aId))
                {
                    return animationObj;
                }
            }
            return  new AnimationObj();
        }

        #endregion


        #region 导入XML
        public void ParseXml(string xmlInfo)
        {

        }

        public static AnimationCfg Get(string xmlInfo)
        {
            while (xmlInfo.StartsWith(" "))
            {
                xmlInfo = xmlInfo.Substring(1);
            }

            if (!xmlInfo.StartsWith("<?xml"))
            {
                return new AnimationCfg();
            }
            else
            {
                xmlInfo = xmlInfo.Replace("\n", "").Replace("\r", "");
            }

            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(xmlInfo);
                XmlElement root = xmlDocument.DocumentElement;
                return Get(root);
            }
            catch
            {
                //Debug.Log("Ex***" + ex);
                return new AnimationCfg();
            }
        }
        public static AnimationCfg Get(XmlElement rootXml)
        {
            var root = new AnimationCfg();
            root.AnimationObjs = AnimationObj.Get(rootXml);
            return root;
        }
        #endregion


        #region 导出XML
        /// <summary>
        /// 导出XML
        /// </summary>
        public string Export_Xml(string filePath)
        {
            //Debug.Log(_filePath);
            XmlDocument xmlMainDocument = new XmlDocument();
            XmlDeclaration xmlDeclaration = xmlMainDocument.CreateXmlDeclaration("1.0", "utf-8", "yes");
            xmlMainDocument.AppendChild(xmlDeclaration);

            XmlNode rootNode = xmlMainDocument.AppendChild(_get_xmlNode(DebugTool.GetObjXml(this), xmlMainDocument));
            xmlMainDocument.AppendChild(rootNode);

            xmlMainDocument.Save(filePath);
            return xmlMainDocument.OuterXml;
        }

        private static XmlNode _get_xmlNode(string xmlInfo, XmlDocument xmlMainDocument)
        {
            XmlDocument xmlItemDocument = new XmlDocument();
            xmlItemDocument.LoadXml(xmlInfo);
            //Debug.Log(xmlInfo);
            if (xmlItemDocument.DocumentElement != null)
            {
                var xmlNode = xmlMainDocument.ImportNode(xmlItemDocument.DocumentElement.FirstChild, true);
                return xmlNode;
            }
            return null;
        }
        #endregion
    }
}
